﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace LDSP.BRL
{
    public partial class Enterprise : BYFW.BOL.BaseConfigEntity
    {
        public Enterprise()
        {
            m_tableName = "t_Enterprise";
            m_hasCode = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected int m_totalDownloadTimes = 0;
        protected int m_downloadTimes = 0;
        protected string m_address = string.Empty;
        protected int m_industryID = 0;
        protected string m_phone = string.Empty;
        protected string m_contact = string.Empty;

        protected Industry m_industry = null;
        protected EnterpriseConfig m_enterpriseConfig = null;


        /// <summary>
        /// 总下载次数
        /// </summary>
        public int TotalDownloadTimes
        {
            get { return m_totalDownloadTimes; }
            set { m_totalDownloadTimes = value; }
        }
        /// <summary>
        /// 当前下载次数
        /// </summary>
        public int DownloadTimes
        {
            get { return m_downloadTimes; }
            set { m_downloadTimes = value; }
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string Address
        {
            get { return m_address; }
            set { m_address = value; }
        }
        /// <summary>
        /// 行业ID
        /// </summary>
        public int IndustryID
        {
            get { return m_industryID; }
            set { m_industryID = value; }
        }
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone
        {
            get { return m_phone; }
            set { m_phone = value; }
        }
        /// <summary>
        /// 联系人
        /// </summary>
        public string Contact
        {
            get { return m_contact; }
            set { m_contact = value; }
        }

        public Industry Industry
        {
            get
            {
                if (m_industry == null)
                {
                    m_industry = new Industry();
                    if (m_industryID != 0)
                    {
                        m_industry.GetProperties(m_industryID);
                    }
                }
                return m_industry;
            }
        }

        public EnterpriseConfig EnterpriseConfig
        {
            get
            {
                if (m_enterpriseConfig == null)
                {
                    m_enterpriseConfig = new EnterpriseConfig(m_id);
                }
                return m_enterpriseConfig;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("totalDownloadTimes", m_totalDownloadTimes);
            m_container.Add("downloadTimes", m_downloadTimes);
            m_container.Add("address", m_address);
            m_container.Add("industryID", m_industryID);
            m_container.Add("phone", m_phone);
            m_container.Add("contact", m_contact);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_totalDownloadTimes = this.GetIntValue(record, "totalDownloadTimes");
            m_downloadTimes = this.GetIntValue(record, "downloadTimes");
            m_address = this.GetStringValue(record, "address");
            m_industryID = this.GetIntValue(record, "industryID");
            m_phone = this.GetStringValue(record, "phone");
            m_contact = this.GetStringValue(record, "contact");
            return true;
        }

        public void ResetDownloadTimes()
        {
            m_downloadTimes = 0;
            this.Update();
        }

        public string GetLogoFileFullName()
        {
            try
            {
                string dir = System.Web.HttpContext.Current.Request.MapPath("~/LogoFiles");
                return System.IO.Path.Combine(dir, m_id + ".png");
            }
            catch { return ""; }
        }

        public override int Delete(string transID)
        {
            //删除文件
            string fileFullPath = this.GetLogoFileFullName();
            if (System.IO.File.Exists(fileFullPath))
            {
                System.IO.File.Delete(fileFullPath);
            }
            //删除企业栏目
            new Catalog().DeleteByEnterprise(transID, m_id);
            //删除文档
            new Codification().DeleteByEnterprise(transID, m_id);
            //删除授权关系
            new R_EnterpriseCatalog().DeleteByEnterprise(transID, m_id);
            //删除适用关系
            new R_EnterpriseCodification().DeleteByEnterprise(transID, m_id);
            //删除企业用户
            new EnterpriseUser().DeleteByEnterprise(transID, m_id);
            return base.Delete(transID);
        }

        public void ChangePrincipal(string old, string current)
        {
            IDataParameter param1 = new SqlParameter("@old_principal", old);
            IDataParameter param2 = new SqlParameter("@new_principal", current);
            DB.ExecuteNonQuery("exec change_codification_principal" + " '" + old + "'," + "'" + current + "','" + ID.ToString() + "'");
        }
    }
}
